package com.xc.ksxt.service;

import com.google.common.collect.Lists;
import com.xc.ksxt.entity.DateScoreEntity;
import com.xc.ksxt.vo.DateScoreVO;
import com.xc.common.object.PageInfo;
import com.xc.common.tool.Helper;
import com.xc.db.tables.DateScore;
import com.xc.db.tables.records.DateScoreRecord;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectForUpdateStep;
import org.jooq.SelectSeekStepN;
import org.jooq.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DateScoreService {
	@Autowired
	private DSLContext dsl;
	private DateScore DATESCORETB = DateScore.DATE_SCORE.as("DATESCORETB");
	public DateScore getTable() {
		return DATESCORETB;
	}
	public Result<Record> select(Condition whr, PageInfo<DateScoreEntity> pageInfo, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(DATESCORETB).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
			pageInfo.setList(result.into(DateScoreEntity.class));
		}
		return result;
	}
	public Result<Record> selectVO(Condition whr, PageInfo<DateScoreVO> pageInfo, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(DATESCORETB).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
			pageInfo.setList(result.into(DateScoreVO.class));
		}
		return result;
	}

	public List<DateScoreEntity> select(Condition whr, SortField<?>... orderby) {
		Result<Record> result = dsl.select().from(DATESCORETB).where(whr).orderBy(orderby).fetch();
		List<DateScoreEntity> list = Lists.newArrayList();
		if (result.size() > 0) {
			list = result.into(DateScoreEntity.class);
		}
		return list;
	}

	public Long count(Condition cond) {
		Long count = dsl.selectCount().from(DATESCORETB).where(cond).fetchOneInto(Long.class);
		return count;
	}

	public Record getRecordById(Long id) {
		Record record = dsl.select().from(DATESCORETB).where(DATESCORETB.ID.eq(id)).fetchOne();
		return record;
	}

	public DateScoreEntity getEntityById(Long id) {
		Record r = getRecordById(id);
		if(r==null) {
			return null;
		}
		DateScoreEntity entity = r.into(DateScoreEntity.class);
		return entity;
	}

	public String update(DateScoreEntity entity) {
		DateScoreRecord record = new DateScoreRecord();
		record.from(entity);
		dsl.executeUpdate(record);
		return entity.getId();
	}

	public String insert(DateScoreEntity entity) {
		DateScoreRecord record = new DateScoreRecord();
        if(StringUtils.isEmpty(entity.getId())) {
        	 String id = Helper.getSnowId() + "";
             entity.setId(id);
        }
        record.from(entity);
        dsl.executeInsert(record);
        return entity.getId();
	}

	public String save(DateScoreEntity entity) {
		if (entity.getId()==null||entity.getId().isEmpty()) {
			return insert(entity);
		}
		return update(entity);
	}

	public void deleteById(Long id) {
		dsl.delete(DATESCORETB).where(DATESCORETB.ID.eq(id)).execute();
	}
}
